package com.laiketui.order.controller;

import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.order.api.dto.StoreOrderDTO;
import com.laiketui.order.api.page.MyPage;
import com.laiketui.order.api.params.mch.MchOrderConfirmParams;
import com.laiketui.order.api.params.pay.BalancePayParams;
import com.laiketui.order.api.params.store.*;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderParent;
import com.laiketui.order.service.*;
import com.laiketui.order.service.pay.BalancePayService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Api("总后台订单列表")
@RestController
@RequestMapping("/v2/store")
public class StoreOrderController {

    @Autowired
    private StoreOrderService storeOrderService;
    @Autowired
    private OrderParentService orderParentService;
    @Autowired
    private OrderService orderService;

    @Autowired
    private BalancePayService balancePayService;
    @Autowired
    private OrderProcessService orderProcessService;
    @Autowired
    private MchOrderService mchOrderService;

    @PostMapping("/list")
    public MyPage<StoreOrderDTO> getOrderList(@RequestBody StoreOrderListParams params) {

        return storeOrderService.getOrderList(params);
    }

    @PostMapping("/info")
    public StoreOrderDTO getOrderDetail(@RequestBody StoreOrderInfoParams params) {
        return storeOrderService.getOrderInfo(params);
    }

    @PostMapping("/offlinePay")
    public String offlinePay(@RequestBody BalancePayParams balancePayParams) {
        OrderParent orderParent = orderParentService.getOrderParent(balancePayParams.getOrderParentNo());
        balancePayParams.setNotUseAmount(true);
        return balancePayService.pay(balancePayParams, orderParent.getUserId(), balancePayParams.getStoreId());
    }

    @PostMapping("/editExpectedDeliveryTime")
    public String editExpectedDeliveryTime(@RequestBody StoreEditExpectedDeliveryTimeParams params) {

        return orderProcessService.editExpectedDeliveryTime(params);
    }

    //修改收货地址
    @PostMapping("/editDelivery")
    public String editDelivery(@RequestBody StoreEditDeliveryParams params) {

        return orderProcessService.editDelivery(params);
    }

    //修改工厂备注
    @PostMapping("/editMchRemark")
    public String editMchRemark(@RequestBody StoreEditMchRemarkParams params) {

        return orderProcessService.editMchRemark(params);
    }

    @PostMapping("/editRemark")
    public String editRemark(@RequestBody StoreEditRemarkParams params) {
        return orderProcessService.editRemark(params);
    }

    @PostMapping("/editDeliverNo")
    public String editDeliverNo(@RequestBody StoreEditDeliverNoParams params) {
        return orderProcessService.editDeliverNo(params);
    }

    @PostMapping("/editQualityInspection")
    public String editQualityInspection(@RequestBody StoreEditQualityInspectionParams params) {
        return orderProcessService.editQualityInspection(params);
    }

    //完成收货
    @PostMapping("/finishOrderParent")
    public boolean finishOrderParent(@RequestBody StoreFinishParams params) {
        return orderProcessService.finish(params);
    }

    //跟踪订单
    @PostMapping("/addOrderParentMessage")
    public boolean addOrderParentMessage(@RequestBody StoreAddParentMessageParams params) {
        return orderProcessService.addOrderParentMessage(params);
    }

    //修改预计发货时间
    @PostMapping("/editEstimatedDeliveryTime")
    public String editEstimatedDeliveryTime(@RequestBody StoreMchOrderConfirmParams params) {
        Order order = orderService.findByOrderNo(params.getOrderNo());
        if (Objects.isNull(order)) {
            throw new LaiKeAPIException("参数错误");
        }
        boolean flag = orderProcessService.updateEstimatedDeliveryTime(order.getOrderParentNo(), params.getEstimatedDeliveryTime());
        if(flag){
            return order.getOrderParentNo();
        }
        return "修改失败";
    }

    //修改质检单
    @PostMapping("/editInspection")
    public boolean editInspection(@RequestBody StoreInspectionParams params) {
        return orderProcessService.editInspection(params);
    }

    //修改发货单
    @PostMapping("/editInvoice")
    public boolean editInvoice(@RequestBody StoreInvoiceParams params) {
        return orderProcessService.editInvoice(params);
    }

    @PostMapping("/editRealDeliveryTime")
    public String editRealDeliveryTime(@RequestBody StoreEditRealDeliveryTimeParams params) {

        return orderProcessService.editRealDeliveryTime(params);
    }

    @PostMapping("/editOrderPriceAndRemark")
    public String editOrderPriceAndRemark(@RequestBody StoreEditOrderPriceParams params){
        return orderProcessService.editOrderPriceAndRemark(params);
    }

    @PostMapping("/editSpecifyDeliveryTime")
    public String editSpecifyDeliveryTime(@RequestBody StoreEditSpecifyDeliveryTimeParams params) {
        return orderProcessService.editSpecifyDeliveryTime(params);
    }

}
